package com.roc.service.merinfo;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.ISQLBaseDao;
import com.roc.pojo.Operators;
import com.roc.service.ServiceException;
import com.roc.web.action.merinfo.MerinfoManageSearchCondition;

@Service("merinfoManageService")
@Transactional(rollbackFor = { ServiceException.class })
public class MerinfoManageServiceImpl implements MerinfoManageService{
	
	
	static Logger logger = LogManager.getLogger(MerinfoManageServiceImpl.class);
	@Autowired
	ISQLBaseDao sqlBaseDao;
	/**
	 * 商户等级变更
	 */

	@Override
	public  Map<String,Object> merinfoChangeGrade(MerinfoManageSearchCondition manageOp,Operators op,String merInfoIdList)
			throws ServiceException {
		//查询商户等级
		String select_mer = " SELECT * FROM tbl_mcht_inf WHERE mcht_cd=? ";
		String updatsql ="update tbl_mcht_inf set isgroup=?,oper_in=?,REC_UPD_TS=?  where mcht_cd=? ";
		
		Object[] params =  new Object[1];
		List<Object> list = new ArrayList<Object>();
		Map<String,Object> map = new HashMap<String,Object>(); 
		try {
			String[] merinfoIdArray = merInfoIdList.split(",");
			for(int i = 0;i<merinfoIdArray.length;i++){
				logger.info("查询tbl_mcht_inf表中是否有商户号为:"+merinfoIdArray[i]+"的商户记录，操作员为:"+op.getRealName());
				params[0] = merinfoIdArray[i];
				Object cc = sqlBaseDao.findObj(select_mer, params);
				
				if(null != cc){
					for(int x=0;x<merinfoIdArray.length;x++) {
				
						list.add(manageOp.getGrade());
						list.add("U"); 
						list.add(new Date()); 
						list.add(merinfoIdArray[x]); 
					
						
						sqlBaseDao.deleteBySql(updatsql, list);
						list.clear();
						logger.info("变更商户表中商户【"+merinfoIdArray[i]+"】的等级，操作员为："+op.getRealName());
					}
					logger.info("商户变更等级成功");
					map.put("success", list);//分配成功信息  
					}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new ServiceException(e.getMessage(),e);
		}
		return map;
    }
   
	/**
	 * 商户关联关系变更
	 */
	
	@Override
	public  Map<String,Object> merinfoChangeAssociation(MerinfoManageSearchCondition manageOp,Operators op,String merInfoIdList)
			throws ServiceException {
		//查询商户等级
		String select_mer = " SELECT * FROM tbl_mcht_inf WHERE mcht_cd=? ";
		String updatsql ="update tbl_mcht_inf set group_cd=?,oper_in=?,REC_UPD_TS=?  where mcht_cd=? ";
		
		Object[] params =  new Object[1];
		List<Object> list = new ArrayList<Object>();
		Map<String,Object> map = new HashMap<String,Object>(); 
		try {
			String[] merinfoIdArray = merInfoIdList.split(",");
			
			for(int i = 0;i<merinfoIdArray.length;i++){
				logger.info("查询tbl_mcht_inf表中是否有商户号为:"+merinfoIdArray[i]+"的商户记录，操作员为:"+op.getRealName());
				params[0] = merinfoIdArray[i];
				Object cc = sqlBaseDao.findObj(select_mer, params);
				if(null != cc){
					for(int x=0;x<merinfoIdArray.length;x++) {
						list.add(manageOp.getAssociation());
						list.add("U"); 
						list.add(new Date()); 
						list.add(merinfoIdArray[x]); 
						sqlBaseDao.deleteBySql(updatsql, list);
						list.clear();
						logger.info("变更商户表中商户【"+merinfoIdArray[i]+"】的关联关系，操作员为："+op.getRealName());
					}
					logger.info("商户变更关联关系成功");
					map.put("success", list);  
					}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new ServiceException(e.getMessage(),e);
		}
		return map;
    }
   

}
